﻿package baidu.reader.common.effects
{
    import baidu.ui.managers.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class PageFlip extends Sprite
    {
        protected var effectPage:Bitmap;
        protected var firstPage:DisplayObject;
        protected var secondPage:DisplayObject;
        protected var effectTemplateMC:MovieClip;
        protected var effectTemplateW:Number;
        protected var effectTemplateH:Number;
        protected var _autoRemove:Boolean;
        protected var effectStarted:Boolean;
        protected var _width:Number;
        protected var _height:Number;

        public function PageFlip()
        {
            this.mouseEnabled = false;
            this.mouseChildren = false;
            this.setTemplate(new EffectTemplateMC(), 500, 300);
            return;
        }// end function

        protected function setTemplate(param1:MovieClip, param2:Number, param3:Number) : void
        {
            if (this.effectTemplateMC)
            {
                removeChild(this.effectTemplateMC);
            }
            this.effectTemplateMC = param1;
            this.effectTemplateMC.gotoAndStop(1);
            this.effectTemplateMC.visible = false;
            addChild(this.effectTemplateMC);
            this.effectTemplateW = param2;
            this.effectTemplateH = param3;
            if (isNaN(this._width))
            {
                this._width = param2;
            }
            else
            {
                this.width = this._width;
            }
            if (isNaN(this._height))
            {
                this._height = param3;
            }
            else
            {
                this.height = this._height;
            }
            return;
        }// end function

        public function startEffect(param1:DisplayObject, param2:Boolean = true, param3:Boolean = false, param4:Boolean = true) : void
        {
            var _loc_6:BitmapData = null;
            this.effectStarted = true;
            this._autoRemove = param4;
            var _loc_5:* = new BitmapData(param1.width, param1.height, true, 0);
            new BitmapData(param1.width, param1.height, true, 0).draw(param1);
            param1 = new Bitmap(_loc_5);
            if (param3)
            {
                _loc_6 = new BitmapData(param1.width * 2, param1.height, true, 2583691263);
            }
            else
            {
                _loc_6 = new BitmapData(param1.width, param1.height, true, 0);
                _loc_6.draw(param1);
            }
            this.effectPage = new Bitmap(_loc_6);
            this.effectPage.smoothing = true;
            this.effectPage.width = this.effectTemplateW;
            this.effectPage.height = this.effectTemplateH;
            this.effectPage.x = (-this.effectTemplateW) / 2;
            this.effectPage.y = -this.effectTemplateH;
            this.firstPage = null;
            this.secondPage = param1;
            this.secondPage.x = param3 ? (param2 ? (0) : (this.effectTemplateW / 2)) : (0);
            this.secondPage.width = this.effectTemplateW * (param3 ? (0.5) : (1));
            this.secondPage.height = this.effectTemplateH;
            if (param2)
            {
                this.effectTemplateMC.scaleX = Math.abs(this.effectTemplateMC.scaleX);
                this.effectTemplateMC.x = 0;
                this.effectPage.scaleX = Math.abs(this.effectPage.scaleX);
                this.effectPage.x = (-this.effectTemplateW) / 2;
                this.secondPage.scaleX = Math.abs(this.secondPage.scaleX);
                this.secondPage.x = 0;
            }
            else
            {
                this.effectTemplateMC.scaleX = Math.abs(this.effectTemplateMC.scaleX) * -1;
                this.effectTemplateMC.x = this.width;
                this.effectPage.scaleX = Math.abs(this.effectPage.scaleX) * -1;
                this.effectPage.x = (-this.effectTemplateW) / 2 + this.effectPage.width;
                this.secondPage.scaleX = Math.abs(this.secondPage.scaleX) * -1;
                this.secondPage.x = this.secondPage.width;
            }
            this.effectTemplateMC.visible = true;
            this.resetEffect();
            this.effectTemplateMC.addEventListener(Event.ENTER_FRAME, this.effectEnterFrameHandler);
            this.effectTemplateMC.addEventListener(Event.FRAME_CONSTRUCTED, this.effectEnterFrameHandler);
            this.relayoutEffectElements();
            this.effectTemplateMC.play();
            dispatchEvent(new PageEffectEvent(PageEffectEvent.EFFECT_START));
            return;
        }// end function

        public function startEffect2(param1:DisplayObject, param2:Boolean = true, param3:Boolean = false, param4:Boolean = true) : void
        {
            var _loc_6:BitmapData = null;
            this._autoRemove = param4;
            this.effectStarted = true;
            var _loc_5:* = new BitmapData(param1.width, param1.height, true, 0);
            new BitmapData(param1.width, param1.height, true, 0).draw(param1);
            param1 = new Bitmap(_loc_5);
            _loc_6 = new BitmapData(param1.width * (param3 ? (2) : (1)), param1.height, true, 2583691263);
            var _loc_7:* = new Matrix();
            if (param2)
            {
                _loc_7.translate(param1.width, 0);
            }
            _loc_6.draw(param1, _loc_7);
            this.effectPage = new Bitmap(_loc_6);
            this.effectPage.smoothing = true;
            this.effectPage.width = this.effectTemplateW;
            this.effectPage.height = this.effectTemplateH;
            this.effectPage.x = (-this.effectTemplateW) / 2;
            this.effectPage.y = -this.effectTemplateH;
            this.secondPage = null;
            this.firstPage = param1;
            this.firstPage.x = param2 ? (0) : (this.effectTemplateW / 2);
            this.firstPage.width = this.effectTemplateW * (param3 ? (0.5) : (1));
            this.firstPage.height = this.effectTemplateH;
            if (param2)
            {
                this.effectTemplateMC.scaleX = Math.abs(this.effectTemplateMC.scaleX);
                this.effectTemplateMC.x = 0;
                this.effectPage.scaleX = Math.abs(this.effectPage.scaleX);
                this.effectPage.x = (-this.effectTemplateW) / 2;
                this.firstPage.scaleX = Math.abs(this.firstPage.scaleX);
                this.firstPage.x = 0;
            }
            else
            {
                this.effectTemplateMC.scaleX = Math.abs(this.effectTemplateMC.scaleX) * -1;
                this.effectTemplateMC.x = this.width;
                this.effectPage.scaleX = Math.abs(this.effectPage.scaleX) * -1;
                this.effectPage.x = (-this.effectTemplateW) / 2 + this.effectPage.width;
                this.firstPage.scaleX = Math.abs(this.firstPage.scaleX) * -1;
                this.firstPage.x = this.firstPage.width;
            }
            this.effectTemplateMC.visible = true;
            this.resetEffect();
            this.effectTemplateMC.addEventListener(Event.ENTER_FRAME, this.effectEnterFrameHandler);
            this.effectTemplateMC.addEventListener(Event.FRAME_CONSTRUCTED, this.effectEnterFrameHandler);
            this.relayoutEffectElements();
            this.effectTemplateMC.play();
            dispatchEvent(new PageEffectEvent(PageEffectEvent.EFFECT_START));
            if (CursorManager.getInstance()._currentCursor)
            {
                DepthManager.bringToTop(CursorManager.getInstance()._currentCursor.parent);
            }
            return;
        }// end function

        private function relayoutEffectElements() : void
        {
            var _loc_1:* = this.effectTemplateMC.getChildByName("firstPage_mc") as MovieClip;
            if (_loc_1)
            {
                while (_loc_1.numChildren > 0)
                {
                    
                    _loc_1.removeChildAt(0);
                }
                if (this.firstPage)
                {
                    _loc_1.addChild(this.firstPage);
                }
            }
            var _loc_2:* = this.effectTemplateMC.getChildByName("secondPage_mc") as MovieClip;
            if (_loc_2)
            {
                while (_loc_2.numChildren > 0)
                {
                    
                    _loc_2.removeChildAt(0);
                }
                if (this.secondPage)
                {
                    _loc_2.addChild(this.secondPage);
                }
            }
            var _loc_3:* = this.effectTemplateMC.getChildByName("effectPage_mc") as MovieClip;
            if (_loc_3)
            {
                while (_loc_3.numChildren > 0)
                {
                    
                    _loc_3.removeChildAt(0);
                }
                _loc_3.addChild(this.effectPage);
            }
            return;
        }// end function

        private function effectEnterFrameHandler(event:Event) : void
        {
            if (event.type == Event.FRAME_CONSTRUCTED)
            {
                this.relayoutEffectElements();
            }
            if (this.effectTemplateMC.currentFrame == this.effectTemplateMC.totalFrames)
            {
                this.effectTemplateMC.removeEventListener(Event.ENTER_FRAME, this.effectEnterFrameHandler);
                this.effectTemplateMC.removeEventListener(Event.FRAME_CONSTRUCTED, this.effectEnterFrameHandler);
                this.effectTemplateMC.stop();
                this.effectTemplateMC.visible = false;
                dispatchEvent(new PageEffectEvent(PageEffectEvent.EFFECT_END));
                if (this._autoRemove && this.parent)
                {
                    this.visible = false;
                    this.parent.removeChild(this);
                }
            }
            return;
        }// end function

        public function resetEffect() : void
        {
            this.effectTemplateMC.removeEventListener(Event.ENTER_FRAME, this.effectEnterFrameHandler);
            this.effectTemplateMC.removeEventListener(Event.FRAME_CONSTRUCTED, this.effectEnterFrameHandler);
            this.effectTemplateMC.gotoAndStop(1);
            return;
        }// end function

        override public function set width(param1:Number) : void
        {
            this._width = param1;
            if (this.effectTemplateMC)
            {
                this.effectTemplateMC.scaleX = this._width / this.effectTemplateW;
            }
            return;
        }// end function

        override public function get width() : Number
        {
            return this._width;
        }// end function

        override public function set height(param1:Number) : void
        {
            this._height = param1;
            if (this.effectTemplateMC)
            {
                this.effectTemplateMC.scaleY = this._height / this.effectTemplateH;
            }
            return;
        }// end function

        override public function get height() : Number
        {
            return this._height;
        }// end function

    }
}
